package com.csun.dsms.service.impl;

import com.csun.dsms.comment.core.support.BaseService;
import com.csun.dsms.mapper.DsmsRoleMenuMapper;
import com.csun.dsms.model.domain.DsmsRoleMenu;
import com.csun.dsms.model.domain.DsmsUser;
import com.csun.dsms.model.vo.menu.MenuVo;
import com.csun.dsms.service.DsmsRoleMenuService;
import com.csun.dsms.utils.TreeUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;

/**
 * @author chisj chisj@foxmail.com
 * @version 1.0.0
 * @date 2021-09-01 13:22
 * @description: TODO
 */
@Service
@Transactional
public class DsmsRoleMenuServiceImpl extends BaseService<DsmsRoleMenu> implements DsmsRoleMenuService {

    @Resource
    private DsmsRoleMenuMapper dsmsRoleMenuMapper;

    /**
     * 查询用户菜单树
     *
     * @param login the login user
     *
     * @return  the user menu vo list
     */
    @Override
    public List<MenuVo> queryOwnMenuTree(DsmsUser login) {

        // 根据机构Id 和 角色Id 获取菜单列表
        List<MenuVo> menuVoList = dsmsRoleMenuMapper
                .queryMenuListByUserId(login.getId());

        return TreeUtil.getChildMenuVos(menuVoList, 0L);
    }

    @Override
    public Integer deleteByRoleIdList(List<Long> roleIdList) {
        return null;
    }

    @Override
    public Integer deleteByRoleId(Long roleId) {

        DsmsRoleMenu dsmsRoleMenu = new DsmsRoleMenu();
        dsmsRoleMenu.setRoleId(roleId);

        return dsmsRoleMenuMapper.delete(dsmsRoleMenu);
    }

    @Override
    public Integer insert(Long roleId, Set<Long> menuIdList) {

        int result = 0;

        DsmsRoleMenu dsmsRoleMenu = new DsmsRoleMenu();
        dsmsRoleMenu.setRoleId(roleId);
        for (Long menuId : menuIdList) {
            dsmsRoleMenu.setMenuId(menuId);
            result += dsmsRoleMenuMapper.insertSelective(dsmsRoleMenu);
        }

        return result;
    }
}
